<template>
    <form-item-wrapper :designer="designer"
                       :field="field"
                       :rules="rules"
                       :design-state="designState"
                       :parent-widget="parentWidget"
                       :parent-list="parentList"
                       :index-of-parent-list="indexOfParentList"
                       :sub-form-row-index="subFormRowIndex"
                       :sub-form-col-index="subFormColIndex"
                       :sub-form-row-id="subFormRowId">
        <el-pagination
                style="text-align: right"
                background
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page_"
                :page-sizes="[10 , 20 , 30 , 40 , 50 , 100]"
                :page-size="size_"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total_">
        </el-pagination>
    </form-item-wrapper>
</template>

<script>
import FormItemWrapper from "@/components/form-designer/form-widget/field-widget/form-item-wrapper.vue";
import emitter from "@/utils/emitter";
import fieldMixin from "@/components/form-designer/form-widget/field-widget/fieldMixin";
import i18n from "@/utils/i18n";
import {EventBus} from "@/components/javaScript/EventBus";

export default {
    name: "el-pagination-widget",
    components: {FormItemWrapper},
    mixins: [emitter, fieldMixin, i18n],
    props: {
        field: Object,
        parentWidget: Object,
        parentList: Array,
        indexOfParentList: Number,
        designer: Object,

        designState: {
            type: Boolean,
            default: false
        },

        subFormRowIndex: { /* 子表单组件行索引，从0开始计数 */
            type: Number,
            default: -1
        },
        subFormColIndex: { /* 子表单组件列索引，从0开始计数 */
            type: Number,
            default: -1
        },
        subFormRowId: { /* 子表单组件行Id，唯一id且不可变 */
            type: String,
            default: ''
        },
        fatherData: {
            type: Object,
            default: () => ({})
        }
    },
    inject: ['refList', 'formConfig', 'globalOptionData', 'globalModel'],
    data() {
        return {
            oldFieldValue: null, //field组件change之前的值
            rules: [],
        }
    },

    computed: {
        options() {
            return this.field.options;
        },
        size_() {
            let s = this.options.pageAttr.size;
            let size = 10;
            if (s && s.length > 0) {
                if (this.fatherData[s[0]]) {
                    size = this.fatherData;
                    for (let i = 0; i < s.length; i++) {
                        size = size[s[i]];
                    }
                }
            }
            return size
        },
        page_() {
            let s = this.options.pageAttr.page;
            let page = 1;
            if (s && s.length > 0) {
                if (this.fatherData[s[0]]) {
                    page = this.fatherData;
                    for (let i = 0; i < s.length; i++) {
                        page = page[s[i]];
                    }
                }
            }
            return page
        },
        total_() {
            let s = this.options.pageAttr.total;
            let total = 10;
            if (s && s.length > 0) {
                if (this.fatherData[s[0]]) {
                    total = this.fatherData;
                    for (let i = 0; i < s.length; i++) {
                        total = total[s[i]];
                    }
                }
            }
            return total
        },
    },

    beforeCreate() {
    },

    created() {
        /* 注意：子组件mounted在父组件created之后、父组件mounted之前触发，故子组件mounted需要用到的prop
           需要在父组件created中初始化！！ */
        this.initFieldModel()
        this.registerToRefList()
        this.initEventHandler()
        this.buildFieldRules()

        this.handleOnCreated()
    },

    mounted() {
        this.handleOnMounted()
    },

    beforeDestroy() {
        this.unregisterFromRefList()
    },

    methods: {
        handleSizeChange(val) {
            if (this.designState) { //设计状态不触发点击事件
                return
            }
            let keyArr = this.options.pageAttr.size;
            let vi = this.fatherData;
            for (let i = 0; i < keyArr.length; i++) {
                if (i !== keyArr.length - 1) {
                    vi = vi[keyArr[i]];
                } else {
                    vi[keyArr[i]] = val;
                }
            }
            let event = `update:${keyArr[0]}`
            this.$emit(event, this.fatherData[keyArr[0]]);

            EventBus.$emit('childEvent', this.field.options.sizeChange);
        },
        handleCurrentChange(val) {
            if (this.designState) { //设计状态不触发点击事件
                return
            }
            let keyArr = this.options.pageAttr.page;
            let vi = this.fatherData;
            for (let i = 0; i < keyArr.length; i++) {
                if (i !== keyArr.length - 1) {
                    vi = vi[keyArr[i]];
                } else {
                    vi[keyArr[i]] = val;
                }
            }
            let event = `update:${keyArr[0]}`
            this.$emit(event, this.fatherData[keyArr[0]]);

            EventBus.$emit('childEvent', this.field.options.pageChange);
        },
    }
}
</script>

<style lang="scss" scoped>
.el-pagination.is-background .el-pager li{
    &.active{
        background-color:$--color-primary !important;
    }
}
</style>
